package com.igoal.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.UserJob;
import com.igoal.entity.UserJobId;
import com.igoal.service.SaveResult;

@Repository("userJobDAO")
public class UserJobDAOImpl extends GenericDaoHibernate<UserJob, UserJobId> implements UserJobDAO {

	public UserJobDAOImpl() {
		super(UserJob.class);
	}

	public int remove(UserJob userJob) {
		try {
			remove(userJob.getId());
		} catch (Exception e) {
			log.error("", e);
			return SaveResult.FAIL;
		}
		return SaveResult.SUCCESS;
	}

	public List<UserJob> getUserJobs(Long userId) {
		DetachedCriteria dc = DetachedCriteria.forClass(UserJob.class);
		dc.add(Restrictions.eq("pk.user.id", userId));
		dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		getByStatus(dc, DataStatus.USING);
		return findByCriteria(dc);
	}

}
